package com.pinyoumei.app.activity.ShoppingCenter.wm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivity;
import com.pinyoumei.app.activity.WebViewActivity;
import com.pinyoumei.app.adapter.WmListAdapter;
import com.pinyoumei.app.bean.WmDetailBean;
import com.pinyoumei.app.bean.WmListBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.OnResponseListener;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.ShareDialog2;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.WXShare;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020-12-22.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-12-22        Administrator             新增Person类
 * 2020-12-22        Administrator             增加sex属性
 * 外卖返现
 */
public class WmActivity extends BaseActivity implements View.OnClickListener {

    private WmListAdapter wmListAdapter;
    private ShareDialog2 dialog;
    private WXShare wxShare;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wm);

        InitWidget();

        InitData();

    }

    private void InitWidget() {

        RelativeLayout  relativeFinish = findViewById(R.id.relative_finish);
        relativeFinish.setOnClickListener(this);


         RecyclerView rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        wmListAdapter = new WmListAdapter(R.layout.item_wm_list,null);
        rv1.setAdapter(wmListAdapter);

        dialog = new ShareDialog2(this);

        wxShare = new WXShare(this);
        wxShare.setListener(new OnResponseListener(){
            @Override
            public void onSuccess(){
                //分享成功
                ToastUtils.showShort("分享成功");
            }
            @Override
            public void onCancel(){
                //分享取消
                ToastUtils.showShort("分享取消");
            }
            @Override
            public void onFail(String message){
                //分享失败
                ToastUtils.showShort("分享失败");
            }
        });

    }

    private void InitData() {

            Map<String, Object> map = new HashMap<>();
            map.put("user_id",  SPUtils.getAppUserId(WmActivity.this));


            OkGo.<String>post(Constants.WM_LIST)
                    .params("user_id",  SPUtils.getAppUserId(WmActivity.this))
                    .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                    .execute(new StringCallback() {




                        @Override
                        public void onStart(Request<String, ? extends Request> request) {
                            super.onStart(request);
                            loadingDailog.show();
                        }


                        @Override
                        public void onSuccess(Response<String> response) {
                            setData(response);

                        }

                        private void setData(Response<String> response) {
                            loadingDailog.dismiss();

                            WmListBean wmListBean = gson.fromJson(response.body(), WmListBean.class);
                            if (wmListBean.getCode() >= 0) {
                                wmListAdapter.setNewData(wmListBean.getResult().getActivity_list());
                                wmListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                        String id = wmListBean.getResult().getActivity_list().get(position).getId();
                                        String share_title = wmListBean.getResult().getActivity_list().get(position).getActivity_title();

                                        switch (view.getId()) {
                                        	case R.id.ll_get:
                                        		//立即领券
                                                SendData(id,1,share_title);
                                        		break;


                                            case R.id.iv_share:
                                                //立即分享

                                                SendData(id,2,share_title);
                                                break;

                                        	default:
                                        		break;
                                        }
                                    }
                                });
                            } else {
                                ToastUtils.showShort(wmListBean.getMsg());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            loadingDailog.dismiss();
                        }
                    });



    }




    private void SendData(String activity_id,int type,String share_title) {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(WmActivity.this));
        map.put("activity_id", activity_id);


        OkGo.<String>post(Constants.WM_DETAIL)
                .params("user_id",  SPUtils.getAppUserId(WmActivity.this))
                .params("activity_id", activity_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {




                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        WmDetailBean wmDetailBean = gson.fromJson(response.body(), WmDetailBean.class);
                        if (wmDetailBean.getCode() >= 0) {
                            switch (type) {
                            	case 1:
                            		//立即领券 h5
                                    Intent intent = new Intent(WmActivity.this, WebViewActivity.class);
                                    intent.putExtra("url", wmDetailBean.getResult().getH5_url());
                                    intent.putExtra("order_title", share_title);
                                    startActivity(intent);

                            		break;
                                case 2:
                                    //立即分享
                                    dialog.setOnClickListener(new ShareDialog2.OnClickListener(){
                                        @Override
                                        public void onClick(View view){
                                            switch (view.getId()){
                                                case R.id.linearLayout_weixinhaoyou://点击分享微信好友(0:好友 1：朋友圈)
                                                    wxShare.shareUrl(0, WmActivity.this,wmDetailBean.getResult().getH5_url(),share_title,wmDetailBean.getResult().getShare_content());
                                                    break;
                                                case R.id.linearLayout_pengyouquan://点击分享朋友圈
                                                    wxShare.shareUrl(1,WmActivity.this,wmDetailBean.getResult().getH5_url(),share_title,wmDetailBean.getResult().getShare_content());
                                                    break;
                                            }
                                        }
                                    });
                                    dialog.show();

                                    break;
                            	default:
                            		break;
                            }
                        } else {
                            ToastUtils.showShort(wmDetailBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        	default:
        		break;
        }
    }
}
